package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service
public class ExtCProductServiceImpl  implements ExtCproductService{


    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ContractDao contractDao;
    @Override
    public void save(ExtCproduct extCproduct) {
        //计算附件的小计金额
        double amount = extCproduct.getPrice() * extCproduct.getCnumber();
        extCproduct.setAmount(amount);
        extCproductDao.insertSelective(extCproduct);

        //修改合同上的附件数+1
        String contractId = extCproduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);

        contract.setExtNum(contract.getExtNum()+1);
        //修改合同上的总金额 +附件的小计金额
        contract.setTotalAmount(contract.getTotalAmount()+amount);
//      更新合同
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public void update(ExtCproduct extCproduct) {

        //计算新的小计金额
        //计算附件的小计金额
        double amount_new = extCproduct.getPrice() * extCproduct.getCnumber();
        //获取旧的小计金额
        //根据附件id  获取到付件
        ExtCproduct cproduct = extCproductDao.selectByPrimaryKey(extCproduct.getId());
//      根据附件获取旧 的小计 金额
        Double amount_old = cproduct.getAmount();
        //设置附件的总金额
        extCproduct.setAmount(amount_new);
        //更新附件
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
        //修改合同上的总金额
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount() + amount_new - amount_old);
        //更新合同
        contractDao.updateByPrimaryKeySelective(contract);


    }

    @Override
    public void delete(String id) {

        //获取附件小计金额
        ExtCproduct cproduct = extCproductDao.selectByPrimaryKey(id);//根据附件id  获取到付件
        Double amount = cproduct.getAmount();//根据附件获取附件小计 金额
        extCproductDao.deleteByPrimaryKey(id);

        //修改合同上的附件数量-1
        Contract contract = contractDao.selectByPrimaryKey(cproduct.getContractId());
        contract.setExtNum(contract.getExtNum()-1);

        //修改合同上的总金额 +附件的小计金额
        contract.setTotalAmount(contract.getTotalAmount()-amount);
//      更新合同
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);

        return new PageInfo(list,5);
    }
}
